# -*- coding: utf-8 -*-
"""
@Author: thekernel
@Date: 2020/2/22 19:43
@Description: 读取 1 - 10 分的数据集
"""
import xlrd
import json

NUM2STR = {
    1: "A",
    2: "B",
    3: "0",
    4: "1",
    5: "2",
    6: "3",
    7: "4",
    8: "5",
    9: "6",
    10: "7",
    11: "8",
    12: "9",
    13: "10"
}


def process_data(filename: str) -> list:
    """
    对指定文件名处理数据
    :param filename:
    :return:
    """
    return_data = list()
    work_sheet = xlrd.open_workbook(filename)
    sheet = work_sheet.sheet_by_index(0)
    row_num = sheet.nrows
    col_num = sheet.ncols
    for i in range(1, row_num):
        single = dict()
        for j in range(1, col_num):
            if sheet.cell(i, j).value != "":
                if NUM2STR[j] not in single.keys():
                    single[NUM2STR[j]] = ""
                single[NUM2STR[j]] += sheet.cell(i, j).value
        return_data.append(single)

    return return_data


if __name__ == '__main__':
    target_file = "data/train.json"
    file_name = "data/数据集汇总第二版（缺121-240-2）.xlsx"

    data = process_data(file_name)
    with open(target_file, "w", encoding="UTF-8") as t:
        for line in data:
            try:
                question = line["A"]
            except Exception as e:
                print(e)
                print(line)
            answer = line["B"]
            for k, v in line.items():
                if k != "A" and k != "B":
                    t.writelines(json.dumps({"A": question, "B": answer, "C": v, "label": int(k)}, ensure_ascii=False))
                    t.writelines("\n")
